package com.tv.cms.payorderinfo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.tv.cms.deviceInfo.model.DeviceInfo;
import com.tv.cms.payorderinfo.mapper.PayOrderInfoMapper;
import com.tv.cms.payorderinfo.model.PayOrderInfo;
import com.tv.cms.payorderinfo.service.PayOrderInfoService;
import com.tv.cms.utils.HttpUtils;
import com.tv.cms.utils.Result;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;

/**
 * TODO
 *
 * @author
 * @date
 **/
@Service
public class PayOrderInfoServiceImp extends MPJBaseServiceImpl<PayOrderInfoMapper, PayOrderInfo> implements PayOrderInfoService {
    @Value("${config.xui-api-key}")
    private String apiKey;
    @Value("${config.xui-api-url}")
    private String apiUrl;
    @Autowired
    private PayOrderInfoMapper mapper;
    @Override
    public List<PayOrderInfo> list(PayOrderInfo payOrderInfo) {
        return mapper.list(payOrderInfo);
    }

    @Override
    public int count(PayOrderInfo payOrderInfo) {
        return mapper.count(payOrderInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateXUI(DeviceInfo deviceInfo) throws IOException {
        Map<String,Object> params = new HashMap<>();
        params.put("api_key",apiKey);
        params.put("id",deviceInfo.getLineId());
        params.put("action","edit_line");
        params.put("bouquets_selected", "["+deviceInfo.getBouquetIds()+"]");
        params.put("username",  deviceInfo.getMac());
        params.put("password",  deviceInfo.getSn());
        params.put("member_id", "1");
        params.put("exp_date", org.apache.http.client.utils.DateUtils.formatDate(deviceInfo.getExpDate(),"yyyy-MM-dd HH:mm:ss") );
        params.put("access_output[]", 1);
        String s = HttpUtils.sendGetRequest3(apiUrl, params);
        System.out.println("XUI==="+s);

    }
}
